//
//  ViewController.m
//  22-QQMusic
//
//  Created by Snow on 16/8/12.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"
#import "AFURLSessionManager.h"
#import "AFURLRequestSerialization.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "NetWorking.h"
#import "MyCollectionViewCell.h"
#import "ModelOfCell.h"
#import "MyCollectionReusableView.h"
#import "GXCycleImage.h"
#import "CollectionReusableView.h"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
//分区头部标题
@property (nonatomic, strong) NSMutableArray *arrayOfSectionTitle;
//轮播图
@property (nonatomic, strong) NSMutableArray *arrayOfScrollView;
//头部视图
@property (nonatomic, strong) NSMutableArray *arrayOfSectionView;
//cell视图
@property (nonatomic, strong) NSMutableArray *arrayOfCellView;
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self handleData];
    [self createCollectionView];
}
- (void)handleData
{
    _arrayOfScrollView = [NSMutableArray array];
    _arrayOfSectionView = [NSMutableArray array];
    _arrayOfSectionTitle =  [NSMutableArray array];
    _arrayOfCellView =  [NSMutableArray array];
    
    [NetWorking GETWithURL:@"http://url.cn/2IcG9OO" parameters:nil success:^(id responseObject) {
    NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSString *str = [string substringFromIndex:7];
    NSString *strNew = [str substringToIndex:str.length - 1];
    NSData *data = [strNew dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dict = [dic objectForKey:@"data"];
        //轮播图
        NSArray *arr = [dict objectForKey:@"mv_show_play"];
        for (NSDictionary *dicti in arr) {
            [_arrayOfScrollView addObject:[dicti objectForKey:@"picurl"]];
        }
        //分区头部
        NSArray *array = [dict[@"contents"] objectForKey:@"list"];
        for (NSDictionary *dictio in array) {
            //头部标题
            [_arrayOfSectionTitle addObject:[dictio objectForKey:@"name"]];
            NSMutableArray *arrayOfTem = [NSMutableArray array];
            NSArray *arrayOfCell = [dictio objectForKey:@"mvlist"];
            for (NSDictionary *diction in arrayOfCell) {
                ModelOfCell *model = [[ModelOfCell alloc] init];
                [model setValuesForKeysWithDictionary:diction];
                [arrayOfTem addObject:model];
            }
            //头部视图
            [_arrayOfSectionView addObject:[arrayOfTem firstObject]];
            [arrayOfTem removeObjectAtIndex:0];
            //cell内容
            [_arrayOfCellView addObject:arrayOfTem];
        }
        //collectionview刷新数据
        [_collectionView reloadData];
    
} filed:^(NSError *error) {
    NSLog(@"请求失败: %@", error);
}];
}
- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 15) / 2, 150);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    _collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_collectionView];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
   [_collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [_collectionView registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reuse"];
    [_collectionView registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _arrayOfSectionTitle.count + 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    } else {
    NSArray *arr = [_arrayOfCellView objectAtIndex:section - 1];
    return arr.count;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSArray *arr = [_arrayOfCellView objectAtIndex:indexPath.section - 1];
    ModelOfCell *model = [arr objectAtIndex:indexPath.row];
    double st = [model.listennum doubleValue] / 10000;
    NSString *str = [NSString stringWithFormat:@"%.2f", st];
    NSString *string = [str stringByAppendingString:@"万"];
    cell.labelOfNum.text = string;
    NSString *stringOfTitile = [model.mvtitle stringByAppendingString:@" - "];
    cell.labelOfTitle.text = [stringOfTitile stringByAppendingString:model.singer_name];
    cell.labelOfContent.text = model.mvdesc;
    if (model.picurl.length != 0) {
        
        [cell.image sd_setImageWithURL:[NSURL URLWithString:model.picurl]];
    } else {
        NSString *strPic = [NSString stringWithFormat:@"http://shp.qpic.cn/qqvideo_ori/0/%@_360_204/0?max_age=259200?max_age=2592000", model.vid];
        [cell.image sd_setImageWithURL:[NSURL URLWithString:strPic]];
    }
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    NSArray *array = @[@"榜单", @"现场", @"频道"];
    NSArray *arr = @[@"Unknown-6", @"Unknown-7", @"Unknown-8"];
    if (indexPath.section == 0) {
        CollectionReusableView *reuseimage = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        reuseimage.backgroundColor = [UIColor clearColor];
        GXCycleImage *image = [[GXCycleImage alloc] initWithFrame:CGRectMake(0, 0, reuseimage.frame.size.width, reuseimage.frame.size.height - 80)];
        NSLog(@"++++++++++++++++++++++%@", _arrayOfScrollView);
        image.arrayImage = _arrayOfScrollView;
        [reuseimage addSubview:image];
        for (int i = 0; i < 3; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(reuseimage.frame.size.width / 3 * i, reuseimage.frame.size.height - 80, reuseimage.frame.size.width / 6, 80);
            [button setImage:[UIImage imageNamed:arr[i]] forState:UIControlStateNormal];
            [reuseimage addSubview:button];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(reuseimage.frame.size.width / 6  + reuseimage.frame.size.width / 3 * i, reuseimage.frame.size.height - 80, reuseimage.frame.size.width / 6, 80)];
            label.text = array[i];
            [reuseimage addSubview:label];
        }
        return reuseimage;
    } else {
        MyCollectionReusableView *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reuse" forIndexPath:indexPath];
        ModelOfCell *model = [_arrayOfSectionView objectAtIndex:indexPath.section - 1];
        reuseView.labelOfHeaderTitle.text = [_arrayOfSectionTitle objectAtIndex:indexPath.section - 1];
        reuseView.labelOfHeaderTitle.textAlignment = 1;
        reuseView.labelOfHeaderTitle.font = [UIFont systemFontOfSize:20];
        double st = [model.listennum doubleValue] / 10000;
        NSString *str = [NSString stringWithFormat:@"%.2f", st];
        NSString *string = [str stringByAppendingString:@"万"];
        reuseView.labelOfNum.text = string;
        reuseView.labelOfNum.textColor = [UIColor whiteColor];
        NSString *stringOfTitile = [model.mvtitle stringByAppendingString:@" - "];
        reuseView.labelOfTitle.text = [stringOfTitile stringByAppendingString:model.singer_name];
        reuseView.labelOfContent.text = model.mvdesc;
        [reuseView.image sd_setImageWithURL:[NSURL URLWithString:model.picurl]];
        return reuseView;
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 300);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
